package com.marketlive.entity;

import org.marketlive.entity.IPrimaryKey;
import org.apache.commons.logging.Log;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.apache.commons.logging.LogFactory;
import java.io.Serializable;

/**
 * An implementation of <code>IPrimaryKey</code> that uses an integer as the underlying identifier.
 */
public class IntegerPk implements IPrimaryKey, Serializable {

    /**
     * Logger.
     */
    private static Log log = LogFactory.getLog(IntegerPk.class);

    /**
     * Type of entity.
     */
    private String entityType = null;

    /**
     * Unique identifier for the entity.
     */
    private int id;

    /**
     * Default constructor. Provided for use by the persistence mechanism.
     */
    public IntegerPk() {
        log.debug("created IntegerPk");
    }

    /**
     * Constructor that fully initializes an <code>IntegerPk</code>.
     *
     * @param entityType the entityType this key is for
     * @param id         the unique identifier of the key
     */
    public IntegerPk(final String entityType, final int id) {
        this.entityType = entityType;
        this.id = id;
        log.debug("created IntegerPk:" + this);
    }

    /**
     * Constructor that fully initializes an <code>IntegerPk</code>.
     *
     * @param entityType the entityType this key is for
     * @param id         the unique identifier. Must represent an integer.
     * @throws NumberFormatException if <code>id</code> cannot be parsed as an integer
     */
    public IntegerPk(final String entityType, final String id) {
        this.entityType = entityType;
        this.id = Integer.parseInt(id);
        log.debug("created IntegerPk:" + this);
    }

    /**
     * {@inheritDoc}
     */
    public String getEntityType() {
        return entityType;
    }


    /**
     * Sets the identifier of the entity this key represents. Used by the persistence mechanism.
     *
     * @param id the unique identifier.  Used by Hibernate.
     */
    private void setEntityID(final int id) {
        this.id = id;
    }

    /**
     * Returns the integer part of this <code>IntegerPk</code>.
     * For example, if the string representation of this key is <code>[Product:12345]</code>,
     * this method returns <code>12345</code>.
     *
     * @return the unique identifier
     */
    public int getEntityID() {
        return id;
    }

    /**
     * Returns the integer part of this key as a <code>String</code>.
     *
     * @return String representation of the integer part of this key
     * @see EntityHome#getPk(String)
     */
    public String getAsString() {
        return Integer.toString(id);
    }

    /**
     * Returns a representation of this <code>IntegerPk</code> useful for debugging.
     * The string has the format <code>entityType:id</code>.
     *
     * @return a representation of this key that is useful for debugging
     */
    public String toString() {
        return this.getEntityType() + ":" + this.id;
    }

    /**
     * Returns true if the given object is equal to this one.
     * Two <code>IntegerPk</code>s are equal if their underlying identifiers are equal.
     *
     * @param o the object to compare with this primary key
     * @return true if the given object is equal to this one
     */
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof IntegerPk)) {
            return false;
        }

        final IntegerPk integerPk = (IntegerPk) o;

        if (id != integerPk.id) {
            return false;
        }
        if (entityType != null ? !entityType.equalsIgnoreCase(integerPk.entityType) : integerPk.entityType != null)
        	return false;
        
        return true;
    }

    /**
     * Returns a hash code for this key.
     * The hash code is the same as the underlying identifier of this <code>IntegerPk</code>.
     *
     * @return hashCode
     * @see #getEntityID()
     */
    public int hashCode() {
        int result = id;      	
        int entityTypeHash = 0;
        if(entityType != null) 
        	entityTypeHash = entityType.toLowerCase().hashCode();
        if(entityTypeHash == 0) {
        	log.debug("entityType was not defined");
        }
        result = 29 * result + entityTypeHash;
        return result;
    }
}
